import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Stream;

/**
 * @Author: 渔小鲤
 * @DateTime: 2025/7/12 18:04
 **/
public class Test {
    public static void main(String[] args) throws IOException {
        Path rootPath = Paths.get("C:\\JAVA\\code\\hudson-mq");

        if (!Files.exists(rootPath)) {
            System.err.println("路径不存在: " + rootPath.toAbsolutePath());
            return;
        }

        // 支持的文件类型映射：后缀 -> 简称
        Map<String, String> fileExtensions = Map.of(
                ".java", "Java",
                ".vue", "Vue",
                ".ts", "TypeScript"
        );

        // 存储统计结果
        Map<String, Long[]> result = new HashMap<>();
        fileExtensions.forEach((ext, name) -> result.put(name, new Long[]{0L, 0L})); // {count, lines}

        try (Stream<Path> walk = Files.walk(rootPath)) {
            walk.filter(path -> !path.toString().contains("\\node_modules\\"))
                    .filter(Files::isRegularFile)
                    .filter(path -> !path.toString().toLowerCase().endsWith("proto.java"))
                    .filter(path -> {
                        String pathStr = path.toString().toLowerCase();
                        return fileExtensions.keySet().stream().anyMatch(ext -> pathStr.endsWith(ext));
                    })
                    .forEach(path -> {
                        String pathStr = path.toString().toLowerCase();
                        for (Map.Entry<String, String> entry : fileExtensions.entrySet()) {
                            String ext = entry.getKey();
                            if (pathStr.endsWith(ext)) {
                                String typeName = entry.getValue();
                                try (Stream<String> lines = Files.lines(path)) {
                                    long lineCount = lines.count();
                                    Long[] stats = result.get(typeName);
                                    stats[0] += 1;
                                    stats[1] += lineCount;
                                } catch (IOException e) {
                                    System.err.println("读取文件失败: " + path);
                                }
                                break;
                            }
                        }
                    });
        } catch (IOException e) {
            System.err.println("遍历目录时发生错误");
            e.printStackTrace();
        }

        // 输出统计结果
        long totalFiles = 0;
        long totalLines = 0;
        for (Map.Entry<String, Long[]> entry : result.entrySet()) {
            String type = entry.getKey();
            Long[] stats = entry.getValue();
            if (stats[0] > 0) {
                System.out.printf("%s 文件总数: %d，总行数: %d\n", type, stats[0], stats[1]);
                totalFiles += stats[0];
                totalLines += stats[1];
            }
        }

        System.out.printf("总计文件数: %d，总代码行数: %d\n", totalFiles, totalLines);

    }
}
